package com.platform.system.service.impl;


import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.constant.Constants;
import com.platform.common.constant.HttpStatus;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.ResultEnum;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.bean.BeanUtils;
import com.platform.common.utils.uuid.SnowflakeIdWorker;
import com.platform.system.domain.dto.device.FirmwareInfoDto;
import com.platform.system.domain.entity.device.FirmwareInfo;
import com.platform.system.domain.entity.device.HumanModelManager;
import com.platform.system.domain.entity.device.UpgradeTask;
import com.platform.system.domain.vo.device.FirmwareInfoVo;
import com.platform.system.domain.vo.device.HumanModelManagerVo;
import com.platform.system.mapper.device.FirmwareInfoMapper;
import com.platform.system.service.ISysFirmwareInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Huangkai
 */
@Service
@Slf4j
public class SysFirmwareInfoServiceImpl extends ServiceImpl<FirmwareInfoMapper, FirmwareInfo> implements ISysFirmwareInfoService {

    @Resource
    FirmwareInfoMapper firmwareInfoMapper;

    @Override
    public TableDataInfo selectFirmwareInfoList(FirmwareInfoVo firmwareInfoVo) {
        LambdaQueryWrapper<FirmwareInfo> firmwareInfoLambdaQueryWrapper = new QueryWrapper<FirmwareInfo>().lambda();
        firmwareInfoLambdaQueryWrapper.like(firmwareInfoVo.getName() != null, FirmwareInfo::getName, firmwareInfoVo.getName());
        firmwareInfoLambdaQueryWrapper.like(firmwareInfoVo.getVersion() != null, FirmwareInfo::getVersion, firmwareInfoVo.getVersion());
        firmwareInfoLambdaQueryWrapper.eq(firmwareInfoVo.getType() != null, FirmwareInfo::getType, firmwareInfoVo.getType());
        firmwareInfoLambdaQueryWrapper.orderByDesc(FirmwareInfo::getCreateTime);
        Page<FirmwareInfo> page = new Page<>();
        page.setCurrent(firmwareInfoVo.getPageNum());
        page.setSize(firmwareInfoVo.getPageSize());
        IPage<FirmwareInfo> fileIPage = firmwareInfoMapper.selectPage(page, firmwareInfoLambdaQueryWrapper);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        List<FirmwareInfoDto> firmwareInfoDtoList = new ArrayList<>();
        if(fileIPage.getRecords() != null && fileIPage.getRecords().size() > 0){
            String json = JSONArray.toJSONString(fileIPage.getRecords());
            firmwareInfoDtoList = JSONArray.parseArray(json, FirmwareInfoDto.class);
            firmwareInfoDtoList.forEach(firmwareInfoDto -> firmwareInfoDto.setVersionName((firmwareInfoDto.getSymbol() + firmwareInfoDto.getVersion())));
        }
        rspData.setRows(firmwareInfoDtoList);
        rspData.setTotal(fileIPage.getTotal());
        return rspData;
    }

    @Override
    public List<FirmwareInfoDto> selectFirmwareInfoByUidList(List<String> uidList) {
        LambdaQueryWrapper<FirmwareInfo> firmwareInfoLambdaQueryWrapper = new QueryWrapper<FirmwareInfo>().lambda();
        firmwareInfoLambdaQueryWrapper.in(FirmwareInfo::getUid, uidList);
        List<FirmwareInfo> firmwareInfoList = firmwareInfoMapper.selectList(firmwareInfoLambdaQueryWrapper);
        if(!CollectionUtils.isEmpty(firmwareInfoList)) {
            String json = JSONArray.toJSONString(firmwareInfoList);
            List<FirmwareInfoDto> firmwareInfoDtoList = JSONArray.parseArray(json, FirmwareInfoDto.class);
            return firmwareInfoDtoList;
        }
        return null;
    }

    @Override
    public FirmwareInfoDto selectFirmwareInfo(String uid) {
        LambdaQueryWrapper<FirmwareInfo> firmwareInfoLambdaQueryWrapper = new QueryWrapper<FirmwareInfo>().lambda();
        firmwareInfoLambdaQueryWrapper.eq(FirmwareInfo::getUid, uid);
        FirmwareInfo firmwareInfo = firmwareInfoMapper.selectOne(firmwareInfoLambdaQueryWrapper);
        if(firmwareInfo != null) {
            FirmwareInfoDto firmwareInfoDto = new FirmwareInfoDto();
            BeanUtils.copyProperties(firmwareInfo, firmwareInfoDto);
            return firmwareInfoDto;
        }
        return null;
    }

    @Override
    public FirmwareInfoDto selectFirmwareInfoByVersion(String version, Integer type) {
        LambdaQueryWrapper<FirmwareInfo> infoLambdaQueryWrapper = new QueryWrapper<FirmwareInfo>().lambda();
        infoLambdaQueryWrapper.eq(FirmwareInfo::getType, type);
        infoLambdaQueryWrapper.like(FirmwareInfo::getVersion, version);
        FirmwareInfo firmwareInfo = firmwareInfoMapper.selectOne(infoLambdaQueryWrapper);
        if(firmwareInfo != null){
            FirmwareInfoDto firmwareInfoDto = new FirmwareInfoDto();
            BeanUtils.copyProperties(firmwareInfo, firmwareInfoDto);
            return firmwareInfoDto;
        }
        return null;
    }

    @Override
    public AjaxResult insertFirmwareInfo(FirmwareInfoVo firmwareInfoVo) {
        FirmwareInfo firmwareInfo = new FirmwareInfo();
        firmwareInfo.setUid(SnowflakeIdWorker.getSnowflakeIdWorker());
        firmwareInfo.setName(firmwareInfoVo.getName());
        firmwareInfo.setSymbol("v");
        firmwareInfo.setVersion(firmwareInfoVo.getVersion());
        firmwareInfo.setType(firmwareInfoVo.getType());
        firmwareInfo.setFileUrl(firmwareInfoVo.getFileUrl());
        firmwareInfo.setRemark(firmwareInfoVo.getRemark());
        firmwareInfo.setSort(Constants.NUM_ZERO);
        firmwareInfo.setIsDel(Constants.NUM_ZERO);
        firmwareInfo.setCreateTime(new Date());
        firmwareInfo.setUpdateTime(new Date());
        firmwareInfo.insert();
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateFirmwareInfo(FirmwareInfoVo firmwareInfoVo) {
        log.info("修改的信息{}", firmwareInfoVo);
        FirmwareInfoDto firmwareInfoDto = selectFirmwareInfo(firmwareInfoVo.getUid());
        if(firmwareInfoDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        LambdaQueryWrapper<FirmwareInfo> firmwareInfoLambdaQueryWrapper = new QueryWrapper<FirmwareInfo>().lambda();
        firmwareInfoLambdaQueryWrapper.eq(FirmwareInfo::getUid, firmwareInfoVo.getUid());
        FirmwareInfo firmwareInfo = new FirmwareInfo();
        BeanUtils.copyProperties(firmwareInfoVo, firmwareInfo);
        firmwareInfo.setUpdateTime(new Date());
        firmwareInfo.update(firmwareInfoLambdaQueryWrapper);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult delFirmwareInfo(FirmwareInfoVo firmwareInfoVo) {
        LambdaQueryWrapper<FirmwareInfo> firmwareInfoLambdaQueryWrapper = new QueryWrapper<FirmwareInfo>().lambda();
        firmwareInfoLambdaQueryWrapper.eq(FirmwareInfo::getUid, firmwareInfoVo.getUid());
        firmwareInfoMapper.delete(firmwareInfoLambdaQueryWrapper);
        return AjaxResult.success();
    }
}
